package com.wechat.service;

import com.github.pagehelper.PageHelper;
import com.wechat.dao.AnswerDao;
import com.wechat.dao.QuestionDao;
import com.wechat.dao.QuestionLibDao;
import com.wechat.dao.UserDao;
import com.wechat.dao.extend.AnswerExtDao;
import com.wechat.dao.extend.QuestionExtDao;
import com.wechat.dao.extend.UserWithdrawcashRecordsExtDao;
import com.wechat.entity.Question;
import com.wechat.entity.QuestionLib;
import com.wechat.entity.User;
import com.wechat.entity.extend.AnswerExt;
import com.wechat.entity.extend.QuestionExt;
import com.wechat.entity.extend.UserWithdrawcashRecordsExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ycx on 2018/4/16.
 */
@Service
public class BackgroundService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private AnswerDao answerDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private AnswerExtDao answerExtDao;
    @Autowired
    private QuestionLibDao questionLibDao;
    @Autowired
    private QuestionExtDao questionExtDao;
    @Autowired
    private UserWithdrawcashRecordsExtDao userWithdrawcashRecordsExtDao;

    /**
     * 题库列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public List<QuestionLib> questionLibList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuestionLib> result = questionLibDao.selQuestionLibList();
        return result;
    }

    /**
     * 题库添加题目
     *
     * @param record
     */
    public int questionSave(QuestionLib record) {
        return questionLibDao.save(record);
    }

    /**
     * 题库编辑题目
     *
     * @param questionLibId
     */
    public QuestionLib questionEdit(Integer questionLibId) {
        return questionLibDao.findById(questionLibId);
    }

    /**
     * 题库更新题目
     *
     * @param record
     */
    public int questionUpdate(QuestionLib record) {
        return questionLibDao.update(record);
    }

    /**
     * 提现记录列表
     *
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param isDesc
     * @return
     */
    public List<UserWithdrawcashRecordsExt> userWithdrawcashRecordsList(Integer pageNum, Integer pageSize, String nickname, Integer isDesc) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserWithdrawcashRecordsExt> result = userWithdrawcashRecordsExtDao.selListByNickname(nickname, isDesc);
        return result;
    }

    /**
     * 出题记录列表
     *
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param id
     * @param isDesc
     * @return
     */
    public List<QuestionExt> questionList(Integer pageNum, Integer pageSize, String nickname, Integer id, Integer isDesc) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuestionExt> result = questionExtDao.selQuestionByNickname(id, nickname, isDesc);
        return result;
    }

    /**
     * 用户管理列表
     *
     * @param pageNum
     * @param pageSize
     * @param nickname
     * @param sort
     * @param isDesc
     * @return
     */
    public List<User> userList(Integer pageNum, Integer pageSize, String nickname, Integer sort, Integer isDesc) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> result = userDao.selByNickname(nickname, sort, isDesc);
        return result;
    }

    /**
     * 答题记录列表
     *
     * @param pageNum
     * @param pageSize
     * @param answerPerson
     * @param questionPerson
     * @param questionId
     * @param isDesc
     * @return
     */
    public List<AnswerExt> answerList(Integer pageNum, Integer pageSize, String answerPerson, String questionPerson, Integer questionId, Integer isDesc) {
        PageHelper.startPage(pageNum, pageSize);
        List<AnswerExt> result = answerExtDao.selList(answerPerson, questionPerson, questionId, isDesc);
        return result;
    }

    /**
     * 通过出题id查询该题目已经被领取了多少红包
     *
     * @param questionId
     * @return
     */
    public double selSumRedPacketByQuestionId(Integer questionId) {
        double result = answerDao.selSumRedPacketByQuestionId(questionId);
        return result;
    }

    public Question selById(Integer id) {
        Question question = questionDao.selById(id);
        return question;
    }

}










































